﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using com.lover.astd.common.config;
using com.lover.astd.common.model;
using com.lover.astd.common.model.enumer;
using com.lover.astd.common;
using com.lover.astd.common.model.building;
using com.lover.astd.common.logic;

namespace com.lover.astd.game.ui.server.impl.economy
{
    public class MovableServer : LogicServer
    {
        public MovableServer(MainForm frm)
        {
            _mainForm = frm; ServerName = ConfigStrings.S_Movable; ServerReadableName = ConfigStrings.SR_Movable;
        }
        public override void renderSettings()
        {
            Dictionary<string, string> conf = getConfig(ServerName);
            if (conf.Count == 0) return;
            MainForm frm = _mainForm;
            frm.chk_movable_enable.Checked = conf.ContainsKey(ConfigStrings.enabled) && conf[ConfigStrings.enabled].ToLower().Equals("true");
            if(conf.ContainsKey(ConfigStrings.order)) frm.txt_movable_order.Text = conf[ConfigStrings.order];
            if (conf.ContainsKey(ConfigStrings.refine_item) && conf[ConfigStrings.refine_item] != "") frm.combo_movable_refine_item.SelectedItem = conf[ConfigStrings.refine_item];

            int wprice = 130;
            if (conf.ContainsKey(ConfigStrings.weave_price))  Int32.TryParse(conf[ConfigStrings.weave_price], out wprice);
            frm.num_movable_weave_price.Value = wprice;
            
            int refine_reserve = 10;
            if (conf.ContainsKey(ConfigStrings.refine_reserve)) Int32.TryParse(conf[ConfigStrings.refine_reserve], out refine_reserve);
            frm.num_movable_refine_reserve.Value = refine_reserve;

            frm.chk_movable_winter_trade.Checked = conf.ContainsKey(ConfigStrings.winter_trade) && conf[ConfigStrings.winter_trade].ToLower().Equals("true");
            frm.chk_movable_arch_trade.Checked = conf.ContainsKey(ConfigStrings.arch_trade) && conf[ConfigStrings.arch_trade].ToLower().Equals("true");
            frm.chk_movable_arch_trade_max.Checked = conf.ContainsKey(ConfigStrings.arch_trade_max) && conf[ConfigStrings.arch_trade_max].ToLower().Equals("true");

            if (conf.ContainsKey(ConfigStrings.trade_visit)) frm.txt_movable_visit.Text = conf[ConfigStrings.trade_visit];
            int visit_fail = 1;
            if (conf.ContainsKey(ConfigStrings.trade_visit_fail)) Int32.TryParse(conf[ConfigStrings.trade_visit_fail], out visit_fail);
            frm.num_movable_visit_fail.Value = visit_fail;
        }
        public override void saveSettings()
        {
            GameConfig conf = getConfig();
            MainForm frm = _mainForm;
            conf.setConfig(ServerName, ConfigStrings.enabled, frm.chk_movable_enable.Checked.ToString());
            conf.setConfig(ServerName, ConfigStrings.order, frm.txt_movable_order.Text);
            if (frm.combo_movable_refine_item.SelectedIndex >= 0)
            {
                conf.setConfig(ServerName, ConfigStrings.refine_item, frm.combo_movable_refine_item.SelectedItem.ToString());
            }
            else
            {
                conf.setConfig(ServerName, ConfigStrings.refine_item, "");
            }
            conf.setConfig(ServerName, ConfigStrings.weave_price, frm.num_movable_weave_price.Value.ToString());
            conf.setConfig(ServerName, ConfigStrings.refine_reserve, frm.num_movable_refine_reserve.Value.ToString());
            conf.setConfig(ServerName, ConfigStrings.winter_trade, frm.chk_movable_winter_trade.Checked.ToString());
            conf.setConfig(ServerName, ConfigStrings.arch_trade, frm.chk_movable_arch_trade.Checked.ToString());
            conf.setConfig(ServerName, ConfigStrings.arch_trade_max, frm.chk_movable_arch_trade_max.Checked.ToString());
            conf.setConfig(ServerName, ConfigStrings.trade_visit, frm.txt_movable_visit.Text);
            conf.setConfig(ServerName, ConfigStrings.trade_visit_fail, frm.num_movable_visit_fail.Value.ToString());
        }
        public override void loadDefaultSettings()
        {
            GameConfig conf = getConfig();
            conf.setConfig(ServerName, ConfigStrings.enabled, "true");
            conf.setConfig(ServerName, ConfigStrings.order, "2341");
            conf.setConfig(ServerName, ConfigStrings.refine_item, "无敌将军炮");
            conf.setConfig(ServerName, ConfigStrings.weave_price, "130");
            conf.setConfig(ServerName, ConfigStrings.refine_reserve, "10");
            conf.setConfig(ServerName, ConfigStrings.winter_trade, "false");
            conf.setConfig(ServerName, ConfigStrings.arch_trade, "false");
            conf.setConfig(ServerName, ConfigStrings.arch_trade_max, "false");
            conf.setConfig(ServerName, ConfigStrings.trade_visit, "");
            conf.setConfig(ServerName, ConfigStrings.trade_visit_fail, "1");
            renderSettings();
        }
    }
}
